package com.example.back.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.back.entity.ChangePassword;
import com.example.back.entity.User;
import com.example.back.exception.ServiceException;
import com.example.back.mapper.UserMapper;
import com.example.back.utils.StringUtil;
import com.example.back.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jm
 * @date 2024/7/12
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User>
{
    @Autowired
    private UserMapper userMapper;

    public static HashMap<Integer, String> roleMap = new HashMap<Integer, String>()
    {
        {
            put(1, "省厅");
            put(2, "地市");
            put(3, "高校");
            put(4, "中小学");
            put(5, "专家");
        }
    };

    public List<User> selectAll()
    {
        return list();
    }


    public void update(User user)
    {
        userMapper.updateById(user);
    }


    public User selectById(Integer id)
    {
        return getById(id);
    }


    public User changePassword(ChangePassword changePassword)
    {
        User user = selectById(changePassword.getId());
        String salt = user.getSalt();
        if(!salt.equals(changePassword.getCode()))
        {
            throw new ServiceException("验证码错误");
        }
        String yuanPassword = StringUtil.md5(changePassword.getPassword() + salt);
        if(!user.getPassword().equals(yuanPassword))
        {
            throw new ServiceException("原密码错误");
        }
        String newSalt = StringUtil.getSalt();
        String newPassword = StringUtil.md5(changePassword.getNewPassword() + newSalt);
        user.setPassword(newPassword);
        user.setSalt(newSalt);
        update(user);

        return user;
    }


    public User login(User user)
    {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        User dbuser = userMapper.selectOne(queryWrapper);
        if(dbuser == null)
        {
            throw new ServiceException("用户名或密码错误");
        }
        String salt = dbuser.getSalt();
        String password = dbuser.getPassword();

        String currentPassword = StringUtil.md5(user.getPassword() + salt);
        if(!password.equals(currentPassword))
        {
            throw new ServiceException("用户名或密码错误");
        }
        //生成token
        String token = TokenUtils.createToken(String.valueOf(dbuser.getId()), dbuser.getPassword());
        dbuser.setToken(token);
        return dbuser;
    }

    public void addUsers(Integer role, Integer num)
    {
        long count = count() + 1;

        for(int i = 0; i < num; i++)
        {
            User user = new User();
            user.setIdentity(role.toString());
            user.setRole(roleMap.get(role));
            String salt = StringUtil.getSalt();
            user.setSalt(salt);
            user.setPassword(StringUtil.md5("123" + salt));
            user.setUsername("user" + count);
            user.setName(roleMap.get(role) + count);
            count += 1;
            userMapper.insert(user);
        }
    }
}
